/*******************************************************************************
 * Package: com.hngtrust.iir.label.controller
 * Type:    LabelController
 * Date:    2021/3/16 16:12
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.framework.page.service.PageService;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.user.controller.dto.DeleteSelfTagVO;
import com.hngtrust.iir.user.controller.dto.EditSelfTagVO;
import com.hngtrust.iir.user.controller.dto.EditTagReqVO;
import com.hngtrust.iir.user.controller.dto.ListSelfTagReqVO;
import com.hngtrust.iir.user.entity.SelfTag;
import com.hngtrust.iir.user.entity.TagRel;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.SelfTagService;
import com.hngtrust.iir.user.service.TagRelService;
import com.hngtrust.iir.user.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * 标签管理
 *
 * @author Keqiwei
 * @date 2021/3/16 16:12
 */
@Slf4j
@Api(tags = "标签管理")
@RestController
@RequestMapping("/label")
public class SelfTagController {
    
    @Autowired
    private SelfTagService selfTagService;
    
    @Autowired
    private PageService pageService;
    
    @Autowired
    private TagService tagService;
    
    @Autowired
    private TagRelService tagRelService;
    @Autowired
    private LogService logService;
    
    /**
     *
     * 主键生成器
     */
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @ApiOperation("标签列表")
    @PostMapping("/list-self-tag")
    public ResponseInfo<PageResult<SelfTag>> listSelfTag(@ApiParam(value = "角色列表") @Valid @RequestBody ReqDTO<ListSelfTagReqVO> req){
        OptLogReqVO vo = new OptLogReqVO();
        vo.setStatus(1);
        if (req.getContent().getTagType() == 1){
            vo.setOptContent("输入"+req.getContent().getName()+"检索企业标签");
        }else if (req.getContent().getTagType() == 2){
            vo.setOptContent("输入"+req.getContent().getName()+"检索新闻标签");
        }else if (req.getContent().getTagType() == 3){
            vo.setOptContent("输入"+req.getContent().getName()+"检索公告标签");
        }else {
            vo.setOptContent("输入"+req.getContent().getName()+"检索研报标签");
        }
        vo.setOptModule(15);
        vo.setOptType(3);
        logService.saveOptLog(vo);
        PageResult<SelfTag> page = pageService.page(req.getPageParam(), () -> tagService.queryList(req.getContent()));
        return new ResponseInfo<>(page);
    }
    
    
    @ApiOperation("全部标签列表")
    @PostMapping("/list-all-tag")
    public ResponseInfo<PageResult<SelfTag>> listAllTag(@ApiParam(value = "角色列表") @Valid @RequestBody ReqDTO<ListSelfTagReqVO> req){
        SelfTag tag = new SelfTag();
        BeanUtils.copyProperties(req.getContent(),tag);
        OptLogReqVO vo = new OptLogReqVO();
        vo.setStatus(1);
        if (req.getContent().getTagType() == 1){
            vo.setOptContent("输入"+req.getContent().getName()+"检索企业标签");
        }else if (req.getContent().getTagType() == 2){
            vo.setOptContent("输入"+req.getContent().getName()+"检索新闻标签");
        }else if (req.getContent().getTagType() == 3){
            vo.setOptContent("输入"+req.getContent().getName()+"检索公告标签");
        }else if (req.getContent().getTagType() == 4){
            vo.setOptContent("输入"+req.getContent().getName()+"检索研报标签");
        }else if (req.getContent().getTagType() == 5){
            vo.setOptContent("输入"+req.getContent().getName()+"检索专家观点标签");
        }
        vo.setOptModule(15);
        vo.setOptType(3);
        logService.saveOptLog(vo);
        PageResult<SelfTag> page = pageService.page(req.getPageParam(), () -> selfTagService.query(tag));
        
        
        return new ResponseInfo(page);
    }
    
    @ApiOperation("删除标签")
    @PostMapping("/delete-self-tag")
    public ResponseInfo<Object> deleteCompLabel(@ApiParam(value = "删除请求") @Valid @RequestBody DeleteSelfTagVO req){
        selfTagService.deleteById(req.getId());
        OptLogReqVO vo = new OptLogReqVO();
        vo.setStatus(1);
        if (req.getTagType() == 1){
            vo.setOptContent("删除公司标签" + req.getName());
        }else if (req.getTagType() == 2){
            vo.setOptContent("删除新闻标签"+req.getName());
        }else if (req.getTagType() == 3){
            vo.setOptContent("删除公告标签"+req.getName());
        }else if (req.getTagType() == 4){
            vo.setOptContent("删除研报标签"+req.getName());
        }else if (req.getTagType() == 5){
            vo.setOptContent("删除专家观点标签"+req.getName());
        }
        vo.setOptModule(15);
        vo.setOptType(8);
        logService.saveOptLog(vo);
        return new ResponseInfo<>();
    }
    
    @ApiOperation("添加标签")
    @PostMapping("/add-self-tag")
    public ResponseInfo<Object> addSelfTag(@ApiParam(value = "标签参数") @Valid @RequestBody EditSelfTagVO req){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        SelfTag selfTag = new SelfTag();
        selfTag.setName(req.getName());
        selfTag.setCreateUserId(loginUser.getId());
        selfTag.setStatus(req.getStatus());
        selfTag.setTagType(req.getTagType());
        ResponseInfo<Object> info = new ResponseInfo<>();
        OptLogReqVO vo = new OptLogReqVO();
        
        if (req.getTagType() == 1){
            vo.setOptContent("添加公司标签" + req.getName());
        }else if (req.getTagType() == 2){
            vo.setOptContent("添加新闻标签"+req.getName());
        }else if (req.getTagType() == 3){
            vo.setOptContent("添加公告标签"+req.getName());
        }else if (req.getTagType() == 4){
            vo.setOptContent("添加研报标签"+req.getName());
        }else if (req.getTagType() == 5){
            vo.setOptContent("添加专家观点标签"+req.getName());
        }
        vo.setOptModule(15);
        vo.setOptType(6);
        if (selfTagService.queryList(selfTag).isEmpty()) {
            BeanUtils.copyProperties(req,selfTag);
            selfTag.setId(myIdGenerator.nextId());
            selfTag.setCreateTime(new Date());
            selfTag.setCreateUserId(loginUser.getId());
            selfTag.setStatus(Constant.DEFAULT_STATUS_NORMAL);
            selfTag.setUpdateUserId(loginUser.getId());
            selfTag.setUpdateTime(new Date());
            selfTagService.add(selfTag);
            vo.setStatus(1);
        }else {
            info.setCode("1");
            info.setMessage("标签已存在");
            vo.setStatus(2);
        }
        logService.saveOptLog(vo);
        return info;
    }
    
    @ApiOperation("修改标签")
    @PostMapping("/update-self-tag")
    public ResponseInfo<Object> updateSelfTag(@ApiParam(value = "标签参数") @Valid @RequestBody EditSelfTagVO req){
        OptLogReqVO vo = new OptLogReqVO();
        vo.setStatus(1);
        if (req.getTagType() == 1){
            vo.setOptContent("编辑公司标签" + req.getName());
        }else if (req.getTagType() == 2){
            vo.setOptContent("编辑新闻标签"+req.getName());
        }else if (req.getTagType() == 3){
            vo.setOptContent("编辑公告标签"+req.getName());
        }else if (req.getTagType() == 4){
            vo.setOptContent("编辑研报标签"+req.getName());
        }else if (req.getTagType() == 5){
            vo.setOptContent("编辑专家观点标签"+req.getName());
        }
        vo.setOptModule(15);
        vo.setOptType(7);
        logService.saveOptLog(vo);
        SelfTag selfTag = new SelfTag();
        BeanUtils.copyProperties(req,selfTag);
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        selfTag.setUpdateTime(new Date());
        selfTag.setUpdateUserId(loginUser.getId());
        selfTagService.modifyById(selfTag);
        return new ResponseInfo<>();
    }
    
    @ApiOperation("添加自定义标签")
    @PostMapping("/add-tag")
    public ResponseInfo<Object> addTag(@ApiParam(value = "添加参数") @Valid @RequestBody EditTagReqVO vo){
        
        TagRel tagRel = new TagRel();
        BeanUtils.copyProperties(vo,tagRel);
        tagRel.setId(myIdGenerator.nextId());
        tagRel.setCreateTime(new Date());
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        tagRel.setCreateUserId(loginUser.getId());
        tagRel.setUpdateUserId(loginUser.getId());
        tagRel.setUpdateTime(new Date());
        tagRelService.add(tagRel);
        return new ResponseInfo<>();
    }
    
    @ApiOperation("删除自定义标签")
    @PostMapping("/delete-tag")
    public ResponseInfo<Object> deleteTag(@ApiParam(value = "删除参数") @Valid @RequestBody EditTagReqVO vo){
        
        TagRel tagRel = new TagRel();
        BeanUtils.copyProperties(vo,tagRel);
        tagRelService.delete(tagRel);
        return new ResponseInfo<>();
    }
}
